﻿namespace HRM.CORE.Process
{
    using DevExpress.XtraBars;
    using DevExpress.XtraEditors.Controls;
    using DevExpress.XtraEditors.Repository;
    using DevExpress.XtraGrid;
    using DevExpress.XtraGrid.Columns;
    using DevExpress.XtraGrid.Views.Grid;
    using HRM.CORE;
    
    using System; using Extra.ERP;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Runtime.CompilerServices;
    using System.Windows.Forms; using Common;
    using APP.HELPER.Utils.Language;
    using DevExpress.XtraGrid.Views.Base;

    public class xucAssignment1 : UserControl
    {
        private BarDockControl barDockControlBottom;
        private BarDockControl barDockControlLeft;
        private BarDockControl barDockControlRight;
        private BarDockControl barDockControlTop;
        private BarManager barManager1;
        private BarButtonItem bbiDelete;
        private GridColumn colAssignmentID;
        private GridColumn colAssignmentName;
        private GridColumn colDate;
        private GridColumn colEmployeeCode;
        private GridColumn colFromDate;
        private GridColumn colMoney;
        private GridColumn colPayMoney;
        private GridColumn colReason;
        private GridColumn colToDate;
        private GridColumn colWhere;
        private IContainer components;
        private GridView gbList;
        private GridControl gcList;
        private string m_EmployeeCode;
        private int m_Month;
        private int m_Year;
        private PopupMenu ppMenu;
        private RepositoryItemCalcEdit repCal;

        public event SavedHandler Saved;

        public xucAssignment1()
        {
            this.components = null;
            this.InitializeComponent();
            this.InitMultiLanguages();
        }

        public xucAssignment1(string EmployeeCode, int Month, int Year)
        {
            this.components = null;
            this.InitializeComponent();
            this.InitMultiLanguages();
            this.Init(this.m_EmployeeCode = EmployeeCode, this.m_Month = Month, this.m_Year = Year);
        }

        private void bbiDelete_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.gbList.DeleteSelectedRows();
        }

        public void ClearData()
        {
            this.gcList.DataSource = null;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void gbList_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                this.gbList.DeleteSelectedRows();
            }
            if (e.KeyCode == Keys.F5)
            {
                this.Init(this.m_EmployeeCode, this.m_Month, this.m_Year);
            }
            if (e.KeyCode == Keys.Escape)
            {
                this.gbList.OptionsView.NewItemRowPosition = NewItemRowPosition.Bottom;
            }
        }

        public void Init(string EmployeeCode, int Month, int Year)
        {
            this.barManager1.SetPopupContextMenu(this.gcList, this.ppMenu);
            this.gcList.DataSource = new HRM_PROCESS_ASSIGNMENT().GetListByMonth(this.m_EmployeeCode = EmployeeCode, this.m_Month = Month, this.m_Year = Year);
        }

        private void InitializeComponent()
        {
            this.components = new Container();
            this.gcList = new GridControl();
            this.gbList = new GridView();
            this.colAssignmentID = new GridColumn();
            this.colEmployeeCode = new GridColumn();
            this.colAssignmentName = new GridColumn();
            this.colReason = new GridColumn();
            this.colWhere = new GridColumn();
            this.colFromDate = new GridColumn();
            this.colToDate = new GridColumn();
            this.colDate = new GridColumn();
            this.colMoney = new GridColumn();
            this.repCal = new RepositoryItemCalcEdit();
            this.colPayMoney = new GridColumn();
            this.barManager1 = new BarManager(this.components);
            this.barDockControlTop = new BarDockControl();
            this.barDockControlBottom = new BarDockControl();
            this.barDockControlLeft = new BarDockControl();
            this.barDockControlRight = new BarDockControl();
            this.bbiDelete = new BarButtonItem();
            this.ppMenu = new PopupMenu(this.components);
            this.gcList.BeginInit();
            this.gbList.BeginInit();
            this.repCal.BeginInit();
            this.barManager1.BeginInit();
            this.ppMenu.BeginInit();
            base.SuspendLayout();
            this.gcList.Dock = DockStyle.Fill;
            this.gcList.Location = new Point(0, 0);
            this.gcList.MainView = this.gbList;
            this.gcList.Name = "gcList";
            this.gcList.RepositoryItems.AddRange(new RepositoryItem[] { this.repCal });
            this.gcList.Size = new Size(0x3a3, 0x1d8);
            this.gcList.TabIndex = 0;
            this.gcList.ViewCollection.AddRange(new BaseView[] { this.gbList });
            this.gbList.BorderStyle = BorderStyles.NoBorder;
            this.gbList.Columns.AddRange(new GridColumn[] { this.colAssignmentID, this.colEmployeeCode, this.colAssignmentName, this.colReason, this.colWhere, this.colFromDate, this.colToDate, this.colDate, this.colMoney, this.colPayMoney });
            this.gbList.GridControl = this.gcList;
            this.gbList.Name = "gbList";
            this.gbList.OptionsSelection.EnableAppearanceFocusedRow = false;
            this.gbList.OptionsView.ColumnAutoWidth = false;
            this.gbList.OptionsView.NewItemRowPosition = NewItemRowPosition.Bottom;
            this.gbList.OptionsView.ShowGroupPanel = false;
            this.gbList.KeyDown += new KeyEventHandler(this.gbList_KeyDown);
            this.colAssignmentID.Caption = "M\x00e3 C\x00f4ng T\x00e1c";
            this.colAssignmentID.FieldName = "AssignmentID";
            this.colAssignmentID.Name = "colAssignmentID";
            this.colAssignmentID.Width = 0x6a;
            this.colEmployeeCode.Caption = "M\x00e3 nh\x00e2n vi\x00ean";
            this.colEmployeeCode.FieldName = "EmployeeCode";
            this.colEmployeeCode.Name = "colEmployeeCode";
            this.colEmployeeCode.Width = 0x6c;
            this.colAssignmentName.Caption = "Đi về việc";
            this.colAssignmentName.FieldName = "AssignmentName";
            this.colAssignmentName.Name = "colAssignmentName";
            this.colAssignmentName.Visible = true;
            this.colAssignmentName.VisibleIndex = 0;
            this.colAssignmentName.Width = 0xc3;
            this.colReason.Caption = "L\x00fd do đi";
            this.colReason.FieldName = "Reason";
            this.colReason.Name = "colReason";
            this.colReason.Visible = true;
            this.colReason.VisibleIndex = 1;
            this.colReason.Width = 190;
            this.colWhere.Caption = "Đi đ\x00e2u";
            this.colWhere.FieldName = "Where";
            this.colWhere.Name = "colWhere";
            this.colWhere.Visible = true;
            this.colWhere.VisibleIndex = 2;
            this.colWhere.Width = 0xc5;
            this.colFromDate.Caption = "Từ ng\x00e0y";
            this.colFromDate.FieldName = "FromDate";
            this.colFromDate.Name = "colFromDate";
            this.colFromDate.Visible = true;
            this.colFromDate.VisibleIndex = 3;
            this.colFromDate.Width = 0x4d;
            this.colToDate.Caption = "Đến ng\x00e0y";
            this.colToDate.FieldName = "ToDate";
            this.colToDate.Name = "colToDate";
            this.colToDate.Visible = true;
            this.colToDate.VisibleIndex = 4;
            this.colDate.Caption = "Ng\x00e0y k\x00fd";
            this.colDate.FieldName = "Date";
            this.colDate.Name = "colDate";
            this.colDate.Visible = true;
            this.colDate.VisibleIndex = 5;
            this.colMoney.Caption = "C\x00f4ng t\x00e1c ph\x00ed";
            this.colMoney.ColumnEdit = this.repCal;
            this.colMoney.FieldName = "Money";
            this.colMoney.Name = "colMoney";
            this.colMoney.Visible = true;
            this.colMoney.VisibleIndex = 6;
            this.colMoney.Width = 0x6f;
            this.repCal.AutoHeight = false;
            this.repCal.Buttons.AddRange(new EditorButton[] { new EditorButton(ButtonPredefines.Combo) });
            this.repCal.Mask.UseMaskAsDisplayFormat = true;
            this.repCal.Name = "repCal";
            this.colPayMoney.Caption = "Đ\x00e3 thanh to\x00e1n";
            this.colPayMoney.ColumnEdit = this.repCal;
            this.colPayMoney.FieldName = "PayMoney";
            this.colPayMoney.Name = "colPayMoney";
            this.colPayMoney.Visible = true;
            this.colPayMoney.VisibleIndex = 7;
            this.colPayMoney.Width = 0x6b;
            this.barManager1.DockControls.Add(this.barDockControlTop);
            this.barManager1.DockControls.Add(this.barDockControlBottom);
            this.barManager1.DockControls.Add(this.barDockControlLeft);
            this.barManager1.DockControls.Add(this.barDockControlRight);
            this.barManager1.Form = this;
            this.barManager1.Items.AddRange(new BarItem[] { this.bbiDelete });
            this.barManager1.MaxItemId = 1;
            this.bbiDelete.Caption = "X\x00f3a";
            this.bbiDelete.Id = 0;
            this.bbiDelete.Name = "bbiDelete";
            this.bbiDelete.ItemClick += new ItemClickEventHandler(this.bbiDelete_ItemClick);
            this.ppMenu.LinksPersistInfo.AddRange(new LinkPersistInfo[] { new LinkPersistInfo(this.bbiDelete) });
            this.ppMenu.Manager = this.barManager1;
            this.ppMenu.Name = "ppMenu";
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.Controls.Add(this.gcList);
            base.Controls.Add(this.barDockControlLeft);
            base.Controls.Add(this.barDockControlRight);
            base.Controls.Add(this.barDockControlBottom);
            base.Controls.Add(this.barDockControlTop);
            base.Name = "xucAssignment1";
            base.Size = new Size(0x3a3, 0x1d8);
            this.gcList.EndInit();
            this.gbList.EndInit();
            this.repCal.EndInit();
            this.barManager1.EndInit();
            this.ppMenu.EndInit();
            base.ResumeLayout(false);
        }

        private void InitMultiLanguages()
        {
            this.colAssignmentName.Caption = MultiLanguages.GetString("tbl_ProcessAssignment", "AssignmentName", this.colAssignmentName.Caption);
            this.colReason.Caption = MultiLanguages.GetString("tbl_ProcessAssignment", "Reason", this.colReason.Caption);
            this.colWhere.Caption = MultiLanguages.GetString("tbl_ProcessAssignment", "Where", this.colWhere.Caption);
            this.colFromDate.Caption = MultiLanguages.GetString("tbl_ProcessAssignment", "FromDate", this.colFromDate.Caption);
            this.colToDate.Caption = MultiLanguages.GetString("tbl_ProcessAssignment", "ToDate", this.colToDate.Caption);
            this.colDate.Caption = MultiLanguages.GetString("tbl_ProcessAssignment", "Date", this.colDate.Caption);
            this.colMoney.Caption = MultiLanguages.GetString("tbl_ProcessAssignment", "Money", this.colMoney.Caption);
        }

        private void RaiseSavedHander()
        {
            if (this.Saved != null)
            {
                this.Saved(this);
            }
        }

        public void Save()
        {
            HRM_PROCESS_ASSIGNMENT hrm_process_assignment = new HRM_PROCESS_ASSIGNMENT();
            hrm_process_assignment.DeleteByMonth(this.m_EmployeeCode, this.m_Month, this.m_Year);
            try
            {
                foreach (DataRow row in (this.gcList.DataSource as DataTable).Rows)
                {
                    decimal money = 0M;
                    decimal payMoney = 0M;
                    decimal debtMoney = 0M;
                    try
                    {
                        money = decimal.Parse(row["Money"].ToString());
                    }
                    catch
                    {
                        money = 0M;
                    }
                    try
                    {
                        payMoney = decimal.Parse(row["PayMoney"].ToString());
                    }
                    catch
                    {
                        payMoney = 0M;
                    }
                    debtMoney = money - payMoney;
                    Guid assignmentID = Guid.NewGuid();
                    hrm_process_assignment.Insert(assignmentID, this.m_EmployeeCode, row["AssignmentName"].ToString(), row["Reason"].ToString(), row["Where"].ToString(), DateTime.Parse(row["FromDate"].ToString()), DateTime.Parse(row["ToDate"].ToString()), money, payMoney, debtMoney, DateTime.Parse(row["Date"].ToString()), "", "", "");
                }
                this.UpdateEmployeeSalary();
                this.RaiseSavedHander();
            }
            catch
            {
            }
        }

        private void UpdateEmployeeSalary()
        {
            HRM_TIMEKEEPER_TABLELIST hrm_timekeeper_tablelist = new HRM_TIMEKEEPER_TABLELIST();
            HRM_SALARY_TABLELIST hrm_salary_tablelist = new HRM_SALARY_TABLELIST();
            hrm_timekeeper_tablelist.Get(this.m_Month, this.m_Year);
            hrm_salary_tablelist.Get(this.m_Month, this.m_Year);
            HRM_SALARY.EmployeeUpdate(hrm_timekeeper_tablelist.TimeKeeperTableListID.ToString(), hrm_salary_tablelist.SalaryTableListID.ToString(), this.m_Month, this.m_Year, this.m_EmployeeCode);
        }

        public delegate void SavedHandler(object sender);
    }
}

